/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tablemodel;

import Entities.Vehiculo;
import ejb.Condicional;
import ejb.Orden;
import ejb.VehiculoFacadeLocal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *Clase que maneja el lazy model de vehiculos.
 * @author Kiki
 */
public class VehiculoLazyModel extends LazyDataModel<Vehiculo> {

    VehiculoFacadeLocal vehiculoFacade = lookupVehiculoFacadeLocal();

    public VehiculoLazyModel(VehiculoFacadeLocal vehiculoFacade) {
//        this.mesaFacade = mesaFacade;
    }

    public Vehiculo getRowData(String rowKey) {
//        for (Mesa car : datasource) {
//            if (car.getNombre().equals(rowKey)) {
//                return car;
//            }
//        }

        return null;
    }

//    @Override
//    public void setRowIndex(final int rowIndex) {
//        if (rowIndex == -1 || getPageSize() == 0) {
//            super.setRowIndex(-1);
//        } else {
//            super.setRowIndex(rowIndex % getPageSize());
//        }
//    }
    @Override
    public Object getRowKey(Vehiculo car) {
        return car.getId();
    }

    @Override
    public List<Vehiculo> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        List<Vehiculo> data;
        try {
            vehiculoFacade.establecerOrdenacion(null, Orden.NINGUNO);
            if (sortField != null && !"".equals(sortField)) {
                switch (sortOrder) {
                    case ASCENDING:
                        vehiculoFacade.establecerOrdenacion(sortField, Orden.ASC);
                        break;
                    case DESCENDING:
                        vehiculoFacade.establecerOrdenacion(sortField, Orden.DESC);
                        break;
                }
            }

            List<Condicional> condicionales = new ArrayList<>();
            if (!filters.isEmpty()) {
                for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                    String filterProperty = it.next();
                    try {
                        String valor = filters.get(filterProperty);
                        switch (filterProperty.toLowerCase()) {
                            case "id":
                                condicionales.add(new Condicional(filterProperty, Integer.valueOf(valor), "="));
                                break;
                            case "activo":
                                if (valor.equals("Inactivo")) {
                                    condicionales.add(new Condicional(filterProperty, true, "="));
                                } else {
                                    condicionales.add(new Condicional(filterProperty, false, "="));
                                }
                                break;
                            default:
                                condicionales.add(new Condicional(filterProperty, valor, "LIKE"));
                        }
                    } catch (Exception e) {
                    }
                }
            }
            if (condicionales.isEmpty()) {
                data = vehiculoFacade.buscarRango(new int[]{first, first + pageSize});
            } else {
                data = vehiculoFacade.buscarConParametros(condicionales, null, new int[]{first, first + pageSize});
            }
            int dataSize = vehiculoFacade.contar();
            this.setRowCount(dataSize);
            return data;
        } catch (Exception ex) {
            Logger.getLogger(ProductosLazyModel.class.getName()).log(Level.SEVERE, null, ex);
            return data = new ArrayList<>();
        }
    }

    private VehiculoFacadeLocal lookupVehiculoFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (VehiculoFacadeLocal) c.lookup("java:global/SGR/SGR-ejb/VehiculoFacade!ejb.VehiculoFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
